import 'package:flutter/material.dart';
import 'package:luckycoffe/common/customAppbar.dart';
import 'package:luckycoffe/common/httpRequst/dioUtils.dart';
import 'package:luckycoffe/common/macros.dart';
import 'package:luckycoffe/pages/menu/widget/product.dart';
import 'package:luckycoffe/pages/shopCar/model/remandModel.dart';
import 'package:luckycoffe/pages/shopCar/model/shopCarModel.dart';
import 'package:luckycoffe/pages/shopCar/model/shopCartData.dart';
import 'package:luckycoffe/pages/shopCar/widget/productBarWidget.dart';
import 'package:provider/provider.dart';
import 'package:oktoast/oktoast.dart';
import '../model/counterModle.dart';
import '../model/remandModel.dart';
import '../widget/remand.dart';

class ShopCarPage extends StatefulWidget {
  @override
  _ShopCarPageState createState() => _ShopCarPageState();
}

class _ShopCarPageState extends State<ShopCarPage> {

  RemandModel _remandModel;

  @override
  void initState() {

   super.initState();


  }

 List<Widget> productList(Map<String,dynamic> dataMap,ShopCarModel shopCarModel){

    List<Widget> shopCarList = [];

    dataMap.keys.forEach((String key) {
      ShoppCartData data = dataMap[key];
      shopCarList.add(
        ProductBarWidget(
          id: data.id.toString(),
          title: data.name,
          price: data.price.toString(),
          nums: data.number,
          formate: data.specName,
          removeAction: (String id){
            setState(() {
              shopCarModel.remove(id);
            });
          },
        )
      );
    });

    return shopCarList;
  }



  @override
  Widget build(BuildContext context) {
    //在页面里初始化Provider,（不要在main文件里操作）

    //从provider获取全局数据
   ShopCarModel shopCarModel = Provider.of<ShopCarModel>(context);
   Map<String,dynamic> dataMap = shopCarModel.shopCartMap;


   print('');
    return Scaffold(
      appBar: CustAppBar(context,title: '购物车'),
      body:Container(
        width: screenW(),
        height: screenH(),
        color: hexf2f2f2(),
        child: Column(
          children: <Widget>[

            SingleChildScrollView(
              child: Container(
                child: Column(
//            children: productList(dataMap,shopCarModel),
                  children: <Widget>[
                    ///顶部图片
                    InkWell(
                      child:Container(
                        child: loadImageAsset(imgPath:'/shopCar/headImage.png',
                            width:screenW(),height: adpHeight(67)),
                      ),
                    ),

                    ///商品列表
                    Container(
                      child: Column(
                        children: productList(dataMap,shopCarModel),
                      ),
                    ),

                    ///猜你喜欢
                RamandWidget(),

                  ],
                ),
              ),
            ),
          ],
        ) ,
      ),
    );
  }
}

class HomePagesec extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomePagesecState();
}

class _HomePagesecState extends State<HomePagesec> {
  @override
  Widget build(BuildContext context) {
    CounterModel counter = Provider.of<CounterModel>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text("第二页"),
      ),
      body: Container(
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20), color: Colors.red),
      ),
    );
  }
}
